package czy.demo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Api(value = "container",tags = {"container"},description = "容器运算",position = 1)
@RestController
@RequestMapping("/container")
public class ContainerController {

    /* responseContainer也没啥作用 */
    @ApiOperation(value = "数组运算",produces = "application/json",responseContainer = "List")
    @ApiImplicitParams({
            /* allowMultiple会使参数可以添加多个，为数组参数 */
            @ApiImplicitParam(name="strings",value = "字符串数组",required = true,dataTypeClass = String.class,allowMultiple = true),
    })
    @GetMapping("/array")
    public String[] array(String[] strings){
        return Arrays.stream(strings).map(String::trim).toArray(String[]::new);
    }

    /* Map作为参数时，需要添加@RequestBody，使swagger将其识别为请求体，相应的响应也会出现 */
    @PostMapping("/map")
    public Map<String,String> map(@RequestBody Map<String,String> map){
        return map;
    }

    /* List也可以识别 */
    @PostMapping("/list")
    public List<String> list(@RequestBody List<String> list){
        return list.stream().map(String::trim).collect(Collectors.toList());
    }

    /* set的形式与list基本一致 */
    @PostMapping("/set")
    public Set<String> set(@RequestBody Set<String> set){
        return set.stream().map(String::trim).collect(Collectors.toSet());
    }



}
